package cn.wanda.bi.sunshine.platform.server.client.wrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import cn.wanda.bi.sunshine.platform.server.SunshineCode;
import cn.wanda.bi.sunshine.platform.server.client.ElasticSearchClient1;
import cn.wanda.bi.sunshine.platform.server.client.ElasticSearchClient2;
import cn.wanda.bi.sunshine.platform.server.client.ElasticSearchConstant;
import cn.wanda.bi.sunshine.platform.server.client.request.elasticsearch.ElasticSearchQuery;
import cn.wanda.bi.sunshine.platform.server.client.request.elasticsearch.ElasticSearchQueryForDistinct;
import cn.wanda.bi.sunshine.platform.server.client.request.elasticsearch.ElasticSearchQueryForDistinctField1Field2;
import cn.wanda.bi.sunshine.platform.server.client.response.elasticsearch.UserLabelBean;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.ListResponse;
import cn.wanda.bi.sunshine.platform.server.utils.JsonUtil;
import cn.wanda.bi.sunshine.platform.server.utils.exception.SunshineException;


/**
 * Created by wangfupeng1
 */
@SuppressWarnings("rawtypes")
@Component
public class ElasticSearchClientWrapper {

	@Value("${client.elasticSearch.url1}")
	String url1;
	@Value("${client.elasticSearch.url2}")
	String url2;
	
    @Autowired
    private ElasticSearchClient1 elasticSearchClient1;
    @Autowired
    private ElasticSearchClient2 elasticSearchClient2;
    
    @Cacheable(value="cache:elasticsearchclientwrapper", key="'cache:elasticsearchclientwrapper:userlabelsearchlistresponse:'+#cacheKey")
	public ListResponse userLabelSearchListResponse(ElasticSearchQuery query,int index, int limit, String cacheKey){
		if(ElasticSearchConstant.clientElasticSearchUrl==null)
			ElasticSearchConstant.clientElasticSearchUrl = url1;
    	//处理返回对象
		UserLabelBean userLabelBean=null;
		try {
			if(ElasticSearchConstant.clientElasticSearchUrl.equals(url1)) {
				String resultUserLabelSearch = elasticSearchClient1.userLabelSearch(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}else {
				String resultUserLabelSearch = elasticSearchClient2.userLabelSearch(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}
		} catch (Exception e) {
			if(ElasticSearchConstant.clientElasticSearchUrl.equals(url1)) {
				ElasticSearchConstant.clientElasticSearchUrl = url2;
				String resultUserLabelSearch = elasticSearchClient2.userLabelSearch(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}else {
				ElasticSearchConstant.clientElasticSearchUrl = url1;
				String resultUserLabelSearch = elasticSearchClient1.userLabelSearch(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}
		}
    	if(null!=userLabelBean.getError())
    		throw new SunshineException(SunshineCode.ELASTICSEARCH_SEARCH_ERROR);
    	return ListResponse.success(userLabelBean.getHits().getHits(), userLabelBean.getHits().getTotal(), index, limit);
    }
    
    @Cacheable(value="cache:elasticsearchclientwrapper", key="'cache:elasticsearchclientwrapper:userlabelbeansearch:'+#cacheKey")
    public UserLabelBean userLabelBeanSearch(ElasticSearchQuery query, String cacheKey){
    	if(ElasticSearchConstant.clientElasticSearchUrl==null)
			ElasticSearchConstant.clientElasticSearchUrl = url1;
    	UserLabelBean userLabelBean = null;
		try {
			if(ElasticSearchConstant.clientElasticSearchUrl.equals(url1)) {
				String resultUserLabelSearch = elasticSearchClient1.userLabelSearch(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}else {
				String resultUserLabelSearch = elasticSearchClient2.userLabelSearch(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}
		} catch (Exception e) {
			if(ElasticSearchConstant.clientElasticSearchUrl.equals(url1)) {
				ElasticSearchConstant.clientElasticSearchUrl = url2;
				String resultUserLabelSearch = elasticSearchClient2.userLabelSearch(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}else {
				ElasticSearchConstant.clientElasticSearchUrl = url1;
				String resultUserLabelSearch = elasticSearchClient1.userLabelSearch(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}
		}
    	if(null!=userLabelBean.getError())
    		throw new SunshineException(SunshineCode.ELASTICSEARCH_SEARCH_ERROR);
    	return userLabelBean;
    }

    @Cacheable(value="cache:elasticsearchclientwrapper", key="'cache:elasticsearchclientwrapper:userlabelsearchfordistinct:'+#cacheKey")
    public UserLabelBean userLabelSearchForDistinct(ElasticSearchQueryForDistinct query, String cacheKey) {
		if(ElasticSearchConstant.clientElasticSearchUrl==null)
			ElasticSearchConstant.clientElasticSearchUrl = url1;
    	UserLabelBean userLabelBean = null;
		try {
			if(ElasticSearchConstant.clientElasticSearchUrl.equals(url1)) {
				String resultUserLabelSearch = elasticSearchClient1.userLabelSearchForDistinct(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}else {
				String resultUserLabelSearch = elasticSearchClient2.userLabelSearchForDistinct(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}
		} catch (Exception e) {
			if(ElasticSearchConstant.clientElasticSearchUrl.equals(url1)) {
				ElasticSearchConstant.clientElasticSearchUrl = url2;
				String resultUserLabelSearch = elasticSearchClient2.userLabelSearchForDistinct(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}else {
				ElasticSearchConstant.clientElasticSearchUrl = url1;
				String resultUserLabelSearch = elasticSearchClient1.userLabelSearchForDistinct(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}
		}
    	if(null!=userLabelBean.getError())
    		throw new SunshineException(SunshineCode.ELASTICSEARCH_SEARCH_ERROR);
    	return userLabelBean;
	}

    @Cacheable(value="cache:elasticsearchclientwrapper", key="'cache:elasticsearchclientwrapper:userlabelsearchfordistinctfield1field2:'+#cacheKey")
    public UserLabelBean userLabelSearchForDistinctField1Field2(ElasticSearchQueryForDistinctField1Field2 query, String cacheKey) {
		if(ElasticSearchConstant.clientElasticSearchUrl==null)
			ElasticSearchConstant.clientElasticSearchUrl = url1;
    	UserLabelBean userLabelBean = null;
		try {
			if(ElasticSearchConstant.clientElasticSearchUrl.equals(url1)) {
				String resultUserLabelSearch = elasticSearchClient1.userLabelSearchForDistinctField1Field2(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}else {
				String resultUserLabelSearch = elasticSearchClient2.userLabelSearchForDistinctField1Field2(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}
		} catch (Exception e) {
			if(ElasticSearchConstant.clientElasticSearchUrl.equals(url1)) {
				ElasticSearchConstant.clientElasticSearchUrl = url2;
				String resultUserLabelSearch = elasticSearchClient2.userLabelSearchForDistinctField1Field2(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}else {
				ElasticSearchConstant.clientElasticSearchUrl = url1;
				String resultUserLabelSearch = elasticSearchClient1.userLabelSearchForDistinctField1Field2(query);
				userLabelBean = JsonUtil.toObject(resultUserLabelSearch, UserLabelBean.class);
			}
		}
		if(null!=userLabelBean.getError())
			throw new SunshineException(SunshineCode.ELASTICSEARCH_SEARCH_ERROR);
		return userLabelBean;
	}

}
